package com.qf.config;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;


/**
 * @author KING
 * @ClassName: com.qf.config.shiro.ShiroConfig
 * @Description: 该类的功能描述
 * @date: 2021/10/25 17:47
 */
@Configuration
public class ShiroConfig {


    /**
     * 3、 ShiroFilterChainDefinition
     *
     * @return
     */
    @Bean
    public ShiroFilterChainDefinition filterChainDefinition() {
        DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();
        // String antPath, url路径
        // String definition  anon   authc
        definition.addPathDefinition("/login", "anon");
        definition.addPathDefinition("/register", "anon");
        return definition;
    }

    /**
     * 2、 DefaultWebSecurityManager  web应用的安全管理器
     */
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(MemberRealm memberRealm, MyDefaultWebSessionManager defaultWebSessionManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(memberRealm);
        //关联realm
        securityManager.setRealm(memberRealm);
        //全局session共享
        securityManager.setSessionManager(defaultWebSessionManager);
        return securityManager;
    }


    /**
     * 1、 Realm   创建realm对象,需要自定义类
     */
    @Bean
    public MemberRealm getMemberRealm() {
        MemberRealm memberRealm = new MemberRealm();
        //修改凭证校验匹配器
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        //设置加密算法
        credentialsMatcher.setHashAlgorithmName("MD5");
        //设置散列次数
        credentialsMatcher.setHashIterations(1024);
        memberRealm.setCredentialsMatcher(credentialsMatcher);

        //开启缓存管理
        memberRealm.setCacheManager(new EhCacheManager());
        //开启全局缓存
        memberRealm.setCachingEnabled(true);
        //开启认证缓存
        memberRealm.setAuthenticationCachingEnabled(true);
        memberRealm.setAuthenticationCacheName("authenticationCache");
        //开启授权缓存
        memberRealm.setAuthorizationCachingEnabled(true);
        memberRealm.setAuthorizationCacheName("authorizationCache");
        return memberRealm;
    }

    /**
     * 全局session共享
     * webSessionManager.setSessionDAO();
     * webSessionManager.setGlobalSessionTimeout();
     * 如果jwt需要设置成false
     * webSessionManager.setSessionIdCookieEnabled(false);
     *
     * @return
     */
    @Bean
    @Primary
    public MyDefaultWebSessionManager sessionManager() {
        MyDefaultWebSessionManager webSessionManager = new MyDefaultWebSessionManager();
        return webSessionManager;
    }
}
